package cn.livingCloud.controller.content.validator;

import cn.livingCloud.dto.ButtonFormDto;
import cn.livingCloud.dto.ButtonFormListDto;
import cn.livingCloud.service.ButtonService;
import cn.livingCloud.wechat.entity.ButtonType;
import cn.livingCloud.wechat.entity.WechatButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.regex.Pattern;

/**
 * Created by sujiantang on 2016/1/8.
 */
@Component
public class ButtonFormListDtoValidator implements Validator {

    public static final String URL_REGULAR = "[a-zA-z]+://[^\\s]*";
    public static final String CHARSET = "UTF-8";
    @Autowired
    private ButtonService service;

    @Override
    public boolean supports(Class<?> aClass) {
        return ButtonFormListDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ButtonFormListDto formListDto = (ButtonFormListDto) target;
        if (!service.checkButtonNumber(formListDto)) {
            errors.rejectValue("addNewButton", null, "按钮数量达到上限,无法添加!");
        }
        try {
            validateButtonList(formListDto, errors, "");
        } catch (Exception e) {
            throw new UnsupportedOperationException("无法获取utf-8编码的字节");
        }
    }


    private void validateButtonList(ButtonFormListDto formListDto, Errors errors, String prefix) throws Exception {
        if (formListDto.getButtonList().size() != 0) {
            for (int i = 0; i < formListDto.getButtonList().size(); i++) {
                String appendString = "buttonList[" + i + "].";
                validateButton(formListDto.getButtonList().get(i), errors, appendString);
            }
        }
        validateButton(formListDto, errors, prefix);
    }


    private void validateButton(ButtonFormDto formDto, Errors errors, String prefix) throws Exception {
        if (StringUtils.isEmpty(formDto.getName())) {
            errors.rejectValue(prefix + "name", null, "按钮名称不能为空");
        }
        if (StringUtils.isEmpty(formDto.getParent_id()) && formDto.getName().getBytes(CHARSET).length > WechatButton.PARENT_BUTTON_NAME_LENGTH) {
            errors.rejectValue(prefix + "name", null, "一级按钮名称不能超过5个字");
        }
        if (!StringUtils.isEmpty(formDto.getParent_id()) && formDto.getName().getBytes(CHARSET).length > WechatButton.CHILD_BUTTON_NAME_LENGTH) {
            errors.rejectValue(prefix + "name", null, "二级按钮名称不能超过13个字");
        }

        ButtonType type = formDto.getType();
        if (type.isClick()) {
            if (!StringUtils.isEmpty(formDto.getUrl())) {
                errors.rejectValue(prefix + "url", null, "点击类型的按钮不能有跳转链接");
            }
            validateButtonKey(formDto, errors, prefix);
        }

        if (type.isView()) {
            if (!StringUtils.isEmpty(formDto.getKey())) {
                errors.rejectValue(prefix + "key", null, "跳转类型的按钮不能填写KEY值");
            }
            validateButtonUrl(formDto, errors, prefix);
        }

    }

    private void validateButtonUrl(ButtonFormDto formDto, Errors errors, String prefix) {
        boolean matches = Pattern.compile(URL_REGULAR).matcher(formDto.getUrl()).matches();

        if (!matches) {
            errors.rejectValue(prefix + "url", null, "请填写正确的URL地址");
        }

    }

    private void validateButtonKey(ButtonFormDto formDto, Errors errors, String prefix) {
        if (StringUtils.isEmpty(formDto.getKey())) {
            errors.rejectValue(prefix + "key", null, "key值不能为空");
        }
        if (formDto.getKey().getBytes().length >= WechatButton.KEY_LENGTH) {
            errors.rejectValue(prefix + "key", null, "key值长度在1-100个字符之间");
        }

    }
}
